package presidents.game;

public class Game {
	private int nrPlayers;
	private Player[] players;
	private Deck deck;
	
	public Game(int nrPlayers, int[] dealSequence){
		this.nrPlayers = nrPlayers;
		initPlayers();
		initDeck();
		deck.shuffle();
		dealCards(dealSequence);
	}

	private void dealCards(int[] dealSequence) {
		int cardsPerPlayer = deck.cardsInDeck() / nrPlayers;
		int cardsLeft = deck.cardsInDeck();
		int cardCounter = 0;
		
		System.out.println("cards per player: " + cardsPerPlayer);
		System.out.print("Deal sequence: ");
		
		int cardsInSeq = 0;
		for(int nr: dealSequence){
			cardsInSeq =+ nr;
			System.out.print(nr + " ");
		}
		
		System.out.println();		
		System.out.println("remainder: " + deck.cardsInDeck() % nrPlayers);
		
		for(int nrCards: dealSequence){
			for(Player player: players){
				for(int i = 0; i < nrCards; i++){
					player.addCard(deck.getCard(cardCounter));
					cardCounter++;
					cardsLeft--;
				}
			}
		}
		
		if(cardsLeft > 0){
			for (int i = 0; i < cardsLeft; i++) {
				players[i].addCard(deck.getCard(cardCounter));
				cardCounter++;
			}
		}
	}
	
	private void initDeck(){
		deck = new Deck(false);
	}

	private void initPlayers() {
		players = new Player[nrPlayers];
		for(int i = 0; i < players.length; i++){
			players[i] = new Player();
		}
	}
	
	public void printPlayerCards(){
		int counter = 0;
		for(Player player : players){
			System.out.println();
			counter++;
			System.out.println("Player " + counter);
			System.out.println("Nr of cards " + player.getCards().size());
			player.printCards();
		}
	}
}